﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;
using System.Data.OracleClient;

namespace OracleDAL.Sec
{

    public class OperateLog
    {
        public OperateLog()
        { }
        #region  成员方法


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string LOGISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sec_OperateLog");
            strSql.Append(" where LOGISN='" + LOGISN + "' ");
            return OracleHelper.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据 LOGISN已经自动生成，不用赋值
        /// </summary>
        public bool Add(Model.Sec.OperateLog model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            Random rand = new Random();
            string strRandTime = model.OPERATIONTIME.ToString("yyyyMMddhhmmss");
            model.LOGISN = strRandTime + rand.Next(0, 999).ToString();
            if (model.LOGISN != null)
            {
                strSql1.Append("LOGISN,");
                strSql2.Append("'" + model.LOGISN + "',");
            }
            if (model.OPERATERESULT != null)
            {
                strSql1.Append("OPERATERESULT,");
                strSql2.Append("'" + model.OPERATERESULT + "',");
            }
            if (model.OPERATEID != null)
            {
                strSql1.Append("OPERATEID,");
                strSql2.Append("'" + model.OPERATEID + "',");
            }
            if (model.BELONGSYSTEM != null)
            {
                strSql1.Append("BELONGSYSTEM,");
                strSql2.Append("'" + model.BELONGSYSTEM + "',");
            }
            if (model.USERISN != null)
            {
                strSql1.Append("USERISN,");
                strSql2.Append("'" + model.USERISN + "',");
            }
            if (model.USERNAME != null)
            {
                strSql1.Append("USERNAME,");
                strSql2.Append("'" + model.USERNAME + "',");
            }
            if (model.OPERATIONTIME != null)
            {
                strSql1.Append("OPERATIONTIME,");
                strSql2.Append("to_date('" + model.OPERATIONTIME + "','yyyy-MM-dd hh24:mi.ss'),");
            }
            if (model.IPADDR != null)
            {
                strSql1.Append("IPADDR,");
                strSql2.Append("'" + model.IPADDR + "',");
            }
            if (model.MACADDR != null)
            {
                strSql1.Append("MACADDR,");
                strSql2.Append("'" + model.MACADDR + "',");
            }
            if (model.OPERATENAME != null)
            {
                strSql1.Append("OPERATENAME,");
                strSql2.Append("'" + model.OPERATENAME + "',");
            }
            if (model.OPERATERESOURCE != null)
            {
                strSql1.Append("OPERATERESOURCE,");
                strSql2.Append("'" + model.OPERATERESOURCE + "',");
            }
            if (model.OPERATECONTENT != null)
            {
                strSql1.Append("OPERATECONTENT,");
                strSql2.Append("'" + model.OPERATECONTENT + "',");
            }
            strSql.Append("insert into sec_OperateLog(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int row=OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
                return true;
            else return false;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Sec.OperateLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update sec_OperateLog set ");
            strSql.Append("OPERATERESULT='" + model.OPERATERESULT + "',");
            strSql.Append("OPERATEID='" + model.OPERATEID + "',");
            strSql.Append("BELONGSYSTEM='" + model.BELONGSYSTEM + "',");
            strSql.Append("USERISN='" + model.USERISN + "',");
            strSql.Append("USERNAME='" + model.USERNAME + "',");
            strSql.Append("OPERATIONTIME='" + model.OPERATIONTIME + "',");
            strSql.Append("IPADDR='" + model.IPADDR + "',");
            strSql.Append("MACADDR='" + model.MACADDR + "',");
            strSql.Append("OPERATENAME='" + model.OPERATENAME + "',");
            strSql.Append("OPERATERESOURCE='" + model.OPERATERESOURCE + "',");
            strSql.Append("OPERATECONTENT='" + model.OPERATECONTENT + "'");
            strSql.Append(" where LOGISN='" + model.LOGISN + "' ");
            int row= OracleHelper.ExecuteSql(strSql.ToString());
            if (row>0)
            {
                return true;
            }
            else return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string LOGISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete sec_OperateLog ");
            strSql.Append(" where LOGISN='" + LOGISN + "' ");
            int row=OracleHelper.ExecuteSql(strSql.ToString());
            if (row > 0)
                return true;
            else return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Sec.OperateLog GetModel(string LOGISN)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" LOGISN,OPERATERESULT,OPERATEID,BELONGSYSTEM,USERISN,USERNAME,OPERATIONTIME,IPADDR,MACADDR,OPERATENAME,OPERATERESOURCE,OPERATECONTENT ");
            strSql.Append(" from sec_OperateLog ");
            strSql.Append(" where LOGISN='" + LOGISN + "' ");
            Model.Sec.OperateLog model = new Model.Sec.OperateLog();
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.LOGISN = ds.Tables[0].Rows[0]["LOGISN"].ToString();
                model.OPERATERESULT = ds.Tables[0].Rows[0]["OPERATERESULT"].ToString();
                model.OPERATEID = ds.Tables[0].Rows[0]["OPERATEID"].ToString();
                model.BELONGSYSTEM = ds.Tables[0].Rows[0]["BELONGSYSTEM"].ToString();
                model.USERISN = ds.Tables[0].Rows[0]["USERISN"].ToString();
                model.USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                if (ds.Tables[0].Rows[0]["OPERATIONTIME"].ToString() != "")
                {
                    model.OPERATIONTIME = DateTime.Parse(ds.Tables[0].Rows[0]["OPERATIONTIME"].ToString());
                }
                model.IPADDR = ds.Tables[0].Rows[0]["IPADDR"].ToString();
                model.MACADDR = ds.Tables[0].Rows[0]["MACADDR"].ToString();
                model.OPERATENAME = ds.Tables[0].Rows[0]["OPERATENAME"].ToString();
                model.OPERATERESOURCE = ds.Tables[0].Rows[0]["OPERATERESOURCE"].ToString();
                model.OPERATECONTENT = ds.Tables[0].Rows[0]["OPERATECONTENT"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LOGISN,OPERATERESULT,OPERATEID,BELONGSYSTEM,USERISN,USERNAME,OPERATIONTIME,IPADDR,MACADDR,OPERATENAME,OPERATERESOURCE,OPERATECONTENT ");
            strSql.Append(" FROM sec_OperateLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}
